package com.micro.frog.acct.rest;

import com.micro.frog.acct.dto.RoleMenuDTO;
import com.micro.frog.acct.dto.UserRoleDTO;
import com.micro.frog.acct.entity.RoleMenu;
import com.micro.frog.acct.entity.UserRole;
import com.micro.frog.acct.service.RoleMenuService;
import com.micro.frog.acct.service.UserRoleService;
import com.micro.frog.core.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *  权限管理
 * @author :  ljh
 * @date : 2018/4/12
 **/
@Slf4j
@RestController
@RequestMapping(value = "/rbac")
public class RbacController {

    @Autowired
    UserRoleService userRoleService;
    @Autowired
    RoleMenuService roleMenuService;
    /**
     * 查询关联角色和用户
     * @return
     */
    @GetMapping(value = "query/roleUser")
    public Result queryUserRole(Integer roleId){
        Result result = new Result();
        try{
            if(roleId == null){
                result.setFlag(false);
                result.setMsg("参数不能为空");
                return result;
            }
            result.setFlag(true);
            result.setMsg("关联成功");
            result.setData(userRoleService.queryRoleUser(roleId));
        }catch (Exception e){
            result.setFlag(false);
            result.setMsg(e.getMessage());
            log.error("关联用户角色错误，e={}",e);
        }
        return result;
    }
    /**
     * 查询关联角色和用户
     * @return
     */
    @GetMapping(value = "query/roleMenu")
    public Result queryRoleMenu(Integer roleId){
        Result result = new Result();
        try{
            if(roleId == null){
                result.setFlag(false);
                result.setMsg("参数不能为空");
                return result;
            }
            result.setFlag(true);
            result.setMsg("关联成功");
            result.setData(roleMenuService.queryRoleMenu(roleId));
        }catch (Exception e){
            result.setFlag(false);
            result.setMsg(e.getMessage());
            log.error("关联用户角色错误，e={}",e);
        }
        return result;
    }
    /**
     * 更新关联角色和用户
     * @return
     */
    @PostMapping(value = "update/roleUser")
    public Result updateUserRole(UserRoleDTO userRoleDTO){
        Result result = new Result();
        try{
            if(userRoleDTO == null
                    || null == userRoleDTO.getRoleId()
                    /*|| StringUtils.isEmpty(userRoleDTO.getUserIds())*/){
                result.setFlag(false);
                result.setMsg("参数不能为空");
                return result;
            }
            userRoleService.updateRoleUsers(userRoleDTO.getRoleId(),userRoleDTO.getUserIds());
            result.setFlag(true);
            result.setMsg("关联成功");
        }catch (Exception e){
            result.setFlag(false);
            result.setMsg(e.getMessage());
            log.error("关联用户角色错误，e={}",e);
        }
        return result;
    }
    /**
     * 更新关联角色和菜单
     * @return
     */
    @PostMapping(value = "update/roleMenu")
    public Result updateRoleMenu(RoleMenuDTO roleMenuDTO){
        Result result = new Result();
        try{
            if(roleMenuDTO == null
                    || null == roleMenuDTO.getRoleId()
                    /*|| StringUtils.isEmpty(roleMenuDTO.getMenuIds())*/){
                result.setFlag(false);
                result.setMsg("参数不能为空");
                return result;
            }
            roleMenuService.updateRoleMenus(roleMenuDTO.getRoleId(),roleMenuDTO.getMenuIds());
            result.setFlag(true);
            result.setMsg("关联成功");
        }catch (Exception e){
            result.setFlag(false);
            result.setMsg(e.getMessage());
            log.error("关联角色菜单错误，e={}",e);
        }
        return result;
    }

}
